package com.enoram.training2.hibernate.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
        	
        	Properties properties = new Properties();
        	try {
        	    properties.load(new FileInputStream("C:\\enoram\\training\\config\\server.properties"));
        	} catch (IOException e) {
        	}
        	
            // Create the SessionFactory from hibernate.cfg.xml
        	Configuration cfg = new Configuration().configure();
        	cfg.setProperty(Environment.URL, properties.getProperty("jdbc.server.url"));
        	cfg.setProperty(Environment.USER, properties.getProperty("jdbc.username"));
        	cfg.setProperty(Environment.PASS, properties.getProperty("jdbc.password"));
        	
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
  

}